<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;

use think\Db;

class Storedetail extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}

	/**
	*车商城车辆详情页面
	*@param   id          车的id      
	**/
	public function showDetail()
	{
		$id = $_GET['id'];
		$carList = Db('store_truck')->where('id',$id)->select();
		//阅读量+1
		$reading  = $carList[0]['reading'] + 1;
		if(Db('store_truck')->where('id', $id)->update(['reading' => $reading])){
			//取出品牌,类型,驱动名字
			$brand = $carList[0]['brand'];
			$type = $carList[0]['type'];
			$drive = $carList[0]['drive'];
			$carList[0]['brand'] = $this->showname($brand);
			$carList[0]['type'] = $this->showname($type);
			$carList[0]['drive'] = $this->showname($drive);

			//找地址
			$carList[0]['site'] = $this->showsite($carList[0]['site']);
			$carList[0]['apply_site'] = $this->showsite($carList[0]['apply_site']);
			$carList[0]['photo'] = unserialize($carList[0]['photo']);
			return $carList;
		}
	}

	/**
	*车商城车辆详情页面购买须知     
	**/
	public function buyKnow()
	{
		$list = Db('store_know')->where('name','store_config')->find();
		$list['value'] = unserialize($list['value'])['content'];
		$list['value'] = htmlspecialchars_decode($list['value']);
		return $list;
	}

	/**
	*车商城车辆详情页面4s店
	**/
	public function storeLink()
	{
		$linklist = Db('store_link')->where('status',1)->order('create_time','asc')->select();
		return $linklist;
	}


	public function detail()
	{
		$detail['car_detail'] = $this->showDetail();
		$detail['fours'] = $this->storeLink();
		$detail['know'] = $this->buyKnow();
		$this->doif($detail);
	}
	/**
	*公共方法:将数组进行判断
	*@param  $arr		  要判断的数组
	**/
	public function doif($arr)
	{
		if($arr){
            $this->apiSuccess($arr);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*取出品牌,类型,驱动名字 
	**/
	public function showname($id)
	{
		$list = Db('store_classify')->where('id',$id)->select();
		$name = $list[0]['name'];
		return $name;
	}

	/**
	*取出地址
	**/
	public function showsite($site)
	{
		$sitearr = explode(',',$site);
		$i = 0;
		for(;$i < count($sitearr);$i++){
			$list = Db('area')->where('id',$sitearr[$i])->select();
			$name = $list[0]['name'];
			$arr[$i] = $name;
		}
		$realname = implode('',$arr);
		return $realname;
	}


}